package cn.ssj.service.impl;

import cn.ssj.dao.BaseDao;
import cn.ssj.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2017-09-10.
 */
public abstract class BaseServiceImpl<T,ID extends Serializable> implements BaseService<T,ID> {

    @Autowired
    private BaseDao<T,ID> dao;

    /**
     * 根据实体使用标记进行查询useFlag
     *
     * @param useFlag 使用标记
     * @return
     */
    public List<T> queryByUseFlag(String useFlag) {
        return dao.queryByUseFlag(useFlag);
    }

    public T insert(T entity) {
        if(null==entity){
            return null;
        }
        return dao.save(entity);
    }


    public T update(T entity) {
        if (null == entity) {
            return null;
        }
        return dao.save(entity);
    }

    public boolean delete(ID id) {
        if(null==id){
            return false;
        }
        dao.delete(id);
        return true;
    }

    public T queryOne(ID id) {
        if(null==id){
            return null;
        }
        return dao.findOne(id);
    }

    public List<T> queryAll() {
        return dao.findAll();
    }

    public Page<T> queryByPage(Integer page, Integer length) {
        if(null==page||null==length){
            return null;
        }
        PageRequest pageRequest=new PageRequest((page-1)*length,length);
        return dao.findAll(pageRequest);
    }
}
